package com.baijiarui.myself;

import com.fc.test.common.base.BaseController;
import com.fc.test.mapper.auto.ForeCompanyMapper;
import com.fc.test.mapper.auto.ForeStudentMapper;
import com.fc.test.model.auto.ForeCompany;
import com.fc.test.model.auto.ForeStudent;
import com.fc.test.model.auto.TsysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/ForeUserRegister")
public class UserRegisterController extends BaseController {
    private String prefix = "other/register";


    @Autowired
    ForeStudentMapper foreStudentMapper;
    @Autowired
    ForeCompanyMapper foreCompanyMapper;
    @RequestMapping("/register")
    public String register(String username,String password,String role){
        TsysUser user=new TsysUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(username);
        List<String> roles=getRole(role);
        sysUserService.insertUserRoles(user,roles);

        //往学生表插入数据插入数据
        if("student".equals(role)){
            ForeStudent foreStudent=new ForeStudent();
            foreStudent.setUsername(username);
            foreStudent.setPassword(password);
            foreStudent.setName(username);
            foreStudent.setId(user.getId());
            foreStudentMapper.insertSelective(foreStudent);
        }
        //往企业表插入数据插入数据
        if("company".equals(role)){
            ForeCompany foreCompany=new ForeCompany();
            foreCompany.setUsername(username);
            foreCompany.setPassword(password);
            foreCompany.setId(user.getId());
            foreCompanyMapper.insertSelective(foreCompany);
        }

        return "index";
    }

    private List<String> getRole(String role) {
        List<String> roles=new ArrayList<>();
        if("student".equals(role)){
            roles.add("414596731276234752");
        }else if("company".equals(role)){
            roles.add("414596821634125824");
        }else if("contentAdmin".equals(role)){
            roles.add("414597395586879488");
        }
        return roles;
    }

}
